<?php

require('mc_table.php');

//koneksi ke database
$host = "localhost";
$user = "root";
$pass = "";
$dbnm = "db_rental_dev";
 
$conn = mysql_connect($host, $user, $pass);
if ($conn) {
	$open = mysql_select_db($dbnm);
	if (!$open) {
		die ("Database tidak dapat dibuka karena ".mysql_error());
	}
} else {
	die ("Server MySQL tidak terhubung karena ".mysql_error());
}
//akhir koneksi

//get query dan variabel
$area = $_GET['area'];

//instansiasi fpdf
$pdf = new PDF_MC_Table('P','cm','A4');
$pdf->AliasNbPages();  
$pdf->AddPage();
$pdf->SetMargins(0.7,0.7,0.7);  

 #tampilkan judul laporan
$pdf->SetFont('Arial','B','16');
$pdf->Cell(0,0, "CIPAGANTI RENTAL CAR & TRAVEL", 0, 1, 'C');
$pdf->Cell(20,2, "STATUS KENDARAAN UNIT TLI", 0, 1, 'C');
$pdf->SetFont('Arial','B','12');
$pdf->SetXY(1.5,3);

$sql = "SELECT area FROM gl_area WHERE id = '$area' ";
$cekdata = mysql_query($sql) or die(mysql_error());
$row_data = mysql_fetch_array($cekdata);
$pdf->Cell(8,3, "Area : ".$row_data['area'],0,0,'L');

//membuat kop tabel    
list($namaarea) = mysql_fetch_row(mysql_query("select area from gl_area where id='$area'"));
$pdf->SetFont('Arial','B',11);
$pdf->SetXY(1.5,5);
$pdf->Cell(2,0.7, "No",1,0,'C');
$pdf->Cell(6,0.7, "STOK STATUS",1,0,'C');
	
		$str = "select id, area from gl_area ";
		if($area!='') $str .= " where id='$area' ";
		$str .= " order by area asc ";
		$query=mysql_query($str) or die (mysql_error());
		while(list($idarea, $namaarea)=mysql_fetch_row($query)){
		
		$pdf->Cell(5,0.7, "$namaarea",1,0,'C');
		}
$pdf->Cell(5,0.7, "TOTAL",1,1,'C');

//ISI TABEL
$pdf->SetFont('Arial','',9);
	$status= array(1=>'Corporate',2=>'Retail',3=>'Tugas Kantor',4=>'Ops / UGC');
	$no=1;
	$totalkeseluruhan = 0;	
	foreach($status as $key=>$value)
	{
	$pdf->SetX(1.5);
	$pdf->Cell(2,0.7, "$no",1,0,'C');
	$pdf->Cell(6,0.7, "$value",1,0,'C');
	
		$str = "select id, area from gl_area ";
		if($area!='') $str .= " where id='$area' ";
		$str .= " order by area asc ";
		$querysub=mysql_query($str) or die (mysql_error());
		$total=0;
		while(list($idarea, $namaarea)=mysql_fetch_row($querysub)){
		list($jmlunit)=mysql_fetch_row(mysql_query("select count(*) from gl_masterkendaraan where area='$idarea' and unitstok='$key'"));		
		
		if($jmlunit==0)$jmlunit='';
		$pdf->Cell(5,0.7, "$jmlunit",1,0,'C');
		
		$total+=$jmlunit;
		}
		if($total==0)$total='';
		$pdf->Cell(5,0.7, "$total",1,1,'C');
		 	
	
	$totalkeseluruhan +=$total;	
	$no++;
	}

$pdf->SetFont('Arial','B',9);	
$pdf->SetX(1.5);
$pdf->Cell(8,0.7, "TOTAL",1,0,'C');

		$str = "select id, area from gl_area ";
		if($area!='') $str .= " where id='$area' ";
		$str .= " order by area asc ";
		$querysub=mysql_query($str) or die (mysql_error());
		$total=0;
		while(list($idarea, $namaarea)=mysql_fetch_row($querysub)){
		list($jmlunit)=mysql_fetch_row(mysql_query("select count(*) from gl_masterkendaraan where area='$idarea'"));		
		
		if($jmlunit==0)$jmlunit='';
		$pdf->Cell(5,0.7, "$jmlunit",1,0,'C');
		$total+=$jmlunit;
		}
		if($total==0)$total='';
			$pdf->Cell(5,0.7, "$total",1,1,'C');

	
	

$pdf->Output('Laporan Status Unit Kendaraan TLI Cabang '.$row_data['area'].'.pdf','D');


?>